package com.itheima.反射reflect.myreflect4;

import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import java.lang.reflect.Field;

public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        //获取class对象
        Class clazz = Class.forName("com.itheima.反射reflect.myreflect4.Student");

    //    method1(clazz);




        Field[] fields1 = clazz.getDeclaredFields();

     //   Field field = clazz.getField("money");

        Field field1 = clazz.getDeclaredField("money");


    }

    private static void method1(Class clazz) throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        //获取所有public修饰的成员变量
        Field field = clazz.getField("name");
        //利用set方法进行赋值
        //先创建一个Student对象
        Student student = (Student) clazz.newInstance();
        //有了对象才可以给指定对象进行赋值
        field.set(student,"zhangsan");

        System.out.println(student);
    }
}

class Student{
    public String name;
    public int age;
    public String gender;
    private int money =300;

}
